
import 'dart:io';
import 'package:flutter_app/class/MyApp11.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

/*App 传感器访问*/
class SensorApp extends StatelessWidget {
  const SensorApp({super.key});

    @override
    Widget build(BuildContext context) {
      // TODO: implement build
      return MaterialApp(
        theme: ThemeData(primarySwatch: Colors.red),
        home: const SensorAppState(key: null),
      );
    }
}


class SensorAppState extends StatefulWidget {
  const SensorAppState({super.key});

  @override
  State<SensorAppState> createState() => _SensorAppState();
}


class _SensorAppState extends State<SensorAppState> {

  late Image _image;

  /*创建按钮*/
  createButtonFor(IconData _icon,String _title,bool _isVertical) {
    //垂直布局
    Object _child = Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Icon(_icon,color: Colors.white),
        Text(
          _title,
          textAlign: TextAlign.right,
          style: const TextStyle(fontSize: 16,fontWeight: FontWeight.bold,color: Colors.white),
        )
      ],
    );

    //水平布局
    if(_isVertical == false) {
      _child = Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
           Icon(_icon,color: Colors.white),
           Text(
            _title,
            textAlign: TextAlign.right,
            style: const TextStyle(fontSize: 16,fontWeight: FontWeight.bold,color: Colors.white),
          )
        ],
      );
    }

    return GestureDetector(
      child: Container(
          padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
          margin: const EdgeInsets.fromLTRB(0, 0, 0, 15),
          alignment: Alignment.center,
          height: 55.0,
          decoration: BoxDecoration(
            color: Colors.deepPurple,
            border: Border.all(color: Colors.black12, width: 1.0),
            borderRadius: BorderRadius.circular(22.5),
          ),
          child: _child as Widget,
      ),
      onTap: (){
        funcButtonAction(_title);
      },
    );
  }

  funcButtonAction(String title){
       switch(title){
         case "相册":
           getImage(true);
           break;
         case "相机":
           getImage(false);
           break;
         case "GPS":
           break;
         case "通讯录":
           break;
       }
  }

  /*
   * 获取图片
   * @params isPhoto: true 相册，反之相机
   */
  Future getImage(bool isPhoto) async {
    //相册
    var pickedFile = await ImagePicker().pickImage(source: ImageSource.gallery);
    if(!isPhoto) {
      pickedFile = await ImagePicker().pickImage(source: ImageSource.camera);
    }

    setState(() {
      if(pickedFile != null){
         _image = Image.file(File(pickedFile.path));

         showDialog(
           context: context,
           builder: (context){
             return const AlertDialog(
               content: Text("获取图片成功"),
             );
           }
         );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: const Text('设备传感器'),
        actions: [
          IconButton(
            icon: const Icon(Icons.chevron_right),
            onPressed:(){
              Navigator.push(context, MaterialPageRoute(builder: (context) => const MyApp11()));
            })
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          textBaseline:TextBaseline.ideographic,
          children: [
             createButtonFor(Icons.photo_album, "相册",true),
             createButtonFor(Icons.photo_camera, "相机",true),
             createButtonFor(Icons.gps_fixed, "GPS",false),
             createButtonFor(Icons.person_add, "通讯录",false),
          ],
        ),
      ),
    );
  }
}